package com.pds.web.spotCheck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.common.utils.SecurityUtils;
import com.pds.web.spotCheck.entity.PdsEnvironmentImprove;
import com.pds.web.spotCheck.mapper.PdsEnvironmentImproveMapper;
import com.pds.web.spotCheck.service.PdsEnvironmentImproveService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 监管部门整改机构表
 *
 * @author srf sunlightcs@gmail.com
 * @since 1.0.0 2024-02-02
 */
@Service("pdsEnvironmentImproveService")
public class PdsEnvironmentImproveServiceImpl extends ServiceImpl<PdsEnvironmentImproveMapper, PdsEnvironmentImprove> implements PdsEnvironmentImproveService {
    @Resource
    private PdsEnvironmentImproveMapper pdsEnvironmentImproveMapper;

    @Override
    public List<PdsEnvironmentImprove> selectList(PdsEnvironmentImprove pdsEnvironmentImprove) {
        QueryWrapper<PdsEnvironmentImprove> wrapper = new QueryWrapper<>();
        Long organizationId = pdsEnvironmentImprove.getOrganizationId();
        Date informDate = pdsEnvironmentImprove.getInformDate();
        String problemItem = pdsEnvironmentImprove.getProblemItem();
        String improvePeriod = pdsEnvironmentImprove.getImprovePeriod();
        if(!Objects.isNull(organizationId)){
            wrapper.eq("organization_id",organizationId);
        }
        if(!Objects.isNull(informDate)){
            wrapper.eq("inform_date",informDate);
        }
        if(!Objects.isNull(problemItem) && !problemItem.isEmpty()){
            wrapper.like("problem_item",problemItem);
        }
        if(!Objects.isNull(improvePeriod) && !improvePeriod.isEmpty()){
            wrapper.eq("improve_period",improvePeriod);
        }
        return pdsEnvironmentImproveMapper.selectList(wrapper);
    }

    @Override
    public PdsEnvironmentImprove queryById(Long id) {
        return pdsEnvironmentImproveMapper.selectById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public PdsEnvironmentImprove insert(PdsEnvironmentImprove pdsEnvironmentImprove) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        /*Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        pdsEnvironmentImprove.setUserId(userId);
        pdsEnvironmentImprove.setCreateBy(nickName);*/
        pdsEnvironmentImprove.setCreateTime(date);
        pdsEnvironmentImprove.setStatus("1");
        pdsEnvironmentImproveMapper.insert(pdsEnvironmentImprove);
        return pdsEnvironmentImprove;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean update(PdsEnvironmentImprove pdsEnvironmentImprove) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        /*String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        pdsEnvironmentImprove.setUpdateBy(nickName);*/
        pdsEnvironmentImprove.setUpdateTime(date);
        return pdsEnvironmentImproveMapper.updateById(pdsEnvironmentImprove) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        return pdsEnvironmentImproveMapper.deleteById(id) > 0;
    }
}